<template>
  <!--  缺寝记录-->
  <div style="margin-top: 60px;margin-left:80px;border: 0 solid red;">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" class="demo-ruleForm" label-width="100px"
             style="margin-left: -40px">
      <el-form-item label="搜索范围:" prop="key">
        <el-select v-model="ruleForm.key" placeholder="请选择范围" style="width: 160px;float: left">
          <el-option label="楼栋" value="buildingName"></el-option>
          <el-option label="宿舍" value="dormitoryName"></el-option>
        </el-select>
      </el-form-item>
      <div style="border: 0 solid red;width: 400px;height: 60px;position: relative;top: -64px;left: 270px">
        <el-form-item label="搜索内容:">
          <el-input v-model="ruleForm.value" placeholder="请输入关键字" style="width: 160px;"></el-input>
          <el-button icon="el-icon-search" style="position: relative;left: 10px;" type="primary"
                     @click="submitForm('ruleForm')">搜索
          </el-button>
        </el-form-item>
      </div>
    </el-form>

    <el-table
        :data="tableData"
        border
        stripe
        style="width: 100%;position: relative;top:-30px">
      <el-table-column
          align="center"
          fixed
          label="ID"
          prop="id"
          sortable
          width="120">
      </el-table-column>
      <el-table-column
          align="center"
          label="楼栋"
          prop="buildingName">
      </el-table-column>
      <el-table-column
          align="center"
          label="宿舍"
          prop="dormitoryName">
      </el-table-column>
      <el-table-column
          align="center"
          label="学生"
          prop="studentName">
      </el-table-column>
      <el-table-column
          align="center"
          label="原因"
          prop="reason">
      </el-table-column>
      <el-table-column
          align="center"
          label="宿管"
          prop="dormitoryAdminName">
      </el-table-column>
      <el-table-column
          align="center"
          label="日期"
          prop="createDate">
      </el-table-column>
    </el-table>
    <el-pagination :current-page.sync="currentPage"
                   :page-size="pageSize"
                   :total="total"
                   background
                   layout="prev, pager, next"
                   style="margin-top: 20px;float: right"
                   @current-change="page">
    </el-pagination>
  </div>

</template>

<script>
export default {
  data() {
    return {
      tableData: null,
      currentPage: 1,
      pageSize: 3,
      total: null,
      key: '',
      value: '',
      ruleForm: {
        key: '',
        value: '',
        page: '',
        size: 3
      },
      rules: {
        key: [
          {required: true, message: '请选择字段', trigger: 'change'}
        ]
      }
    }
  },
  methods: {
    submitForm(formName) {
      const _this = this
      //让翻页复原
      _this.currentPage = 1
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const _this = this
          _this.ruleForm.page = _this.currentPage
          axios.get('http://localhost:9090/absent/search', {params: _this.ruleForm}).then(function (resp) {
            _this.tableData = resp.data.data.data
            _this.total = resp.data.data.total
          })
        }
      });

    },
    page(currentPage) {
      const _this = this
      if (_this.ruleForm.value === '') {
        axios.get('http://localhost:9090/absent/list/' + currentPage + '/' + _this.pageSize).then(function (resp) {
          _this.tableData = resp.data.data.data
          _this.total = resp.data.data.total
        })
      } else {
        _this.ruleForm.page = _this.currentPage
        axios.get('http://localhost:9090/absent/search', {params: _this.ruleForm}).then(function (resp) {
          _this.tableData = resp.data.data.data
          _this.total = resp.data.data.total
        })
      }

    }
  },
  created() {
    const _this = this
    axios.get('http://localhost:9090/absent/list/1/' + _this.pageSize).then(function (resp) {
      _this.tableData = resp.data.data.data
      _this.total = resp.data.data.total
    })
  }
}
</script>